# -*- coding: utf-8 -*-
#  rat@kitchen
#  


import simplejson, serial, socket
import math
import time


from arduino import *
import ast



sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
file = sock.makefile("rb")
sock.connect(("192.168.0.5", 8000))
sock.send("GET /?continuous=1&delay=0 HTTP/1.0\r\n\r\n")




pitch=0
roll=0
servos=(9,10,11)
board=init_board(servos)
#change_speed(250)




pitchStart=90
rollStart=90


while True:
    try:
        string = file.readline()
    except ValueError:
        continue;
    try:
        result=ast.literal_eval(string)
    except:
        result={}
    for k,v in result.iteritems():
        if k==u"pitch":
            pitch =int(float(v/(math.pi/2))*100)
        if k==u"roll":
            roll =int(float(v/(math.pi/2))*100)+45
    try:
        speed=smooth(roll,valueMax=255,valueNull=50)
        pitch=pitchStart-smooth(pitch)
        roll=rollStart+smooth(roll)

        
        print roll, pitch, speed
        change_pitch(pitch)
        change_roll(roll)
        #change_speed(speed)
    except:
        pass
   
   
   
   


